import { HTTP } from '../utils/http.js'
class ClassicModel extends HTTP {
    getLatest(sCallBack) {
        this.request({
            url: 'classic/latest',
            success: (res)=>{
              sCallBack(res)
              this._setLatestIndex(res.data.index)
              let key = this._getKey(res.data.index)
              wx.setStorageSync(key,res)
            }
          })
    }
    onLike(behavior, artID, category) {
      let url = behavior=='like'? 'like': 'like/cancel'
      this.request({
        method: 'POST',
        url,
        data: {
          art_id: artID,
          type: category
        }
      })
    }
    getClassic(index,nextOrPrevious, sCallBack) {
      let key = nextOrPrevious=='next'? this._getKey(index+1):this._getKey(index - 1)
      let classic = wx.getStorageSync(key)
      if(!classic) {
        this.request({
          url: `classic/${index}/${nextOrPrevious}`,
          success: (res)=>{
            wx.setStorageSync(this._getKey(res.data.index),res)
            sCallBack(res)
          }
        })
      }
      else{
        sCallBack(classic)
      }
    }
    isFirst(index) {
      return index == 1 ? true: false
    }
    isLatest(index) {
      let latestIndex = this._getLatestIndex()
      return index == latestIndex  ? true: false
    }
    _setLatestIndex(index) {
      wx.setStorageSync('latestIndex',index)
    }
    _getLatestIndex() {
      let latest = wx.getStorageSync('latestIndex')
      return latest
    }
    _getKey(index) {
      let key = 'classic-' +index
      return key
    }
    getClassicLikeStatus(artID,category,sCallBack){
      this.request({
        url: `classic/${category}/${artID}/favor`,
        success: sCallBack
      })
    }
    getMyFavor(success){
      this.request({
        url: 'classic/favor',
        success: success
      })
    }
}
export { ClassicModel }